//
//  MyPlayerController.m
//  MyMoviePlayer
//
//  Created by wangbo on 2/6/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "MyRequest.h"
#import "XMLParser.h"
#import "MyPlayerController.h"
#import "ASIHTTPRequest.h"

static MyPlayerController *playerController;
static MPMoviePlayerController *player;

@implementation MyPlayerController
//@synthesize player;
#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

+(MyPlayerController*)shareInstance{
    if(!playerController){
        playerController=[[MyPlayerController alloc]init];
    }
    return playerController;
}
-(void) playerCallback:(NSNotification *)sender{
    MPMoviePlayerController *theMovie=[sender object];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];
    [theMovie.view removeFromSuperview];
    [self dismissModalViewControllerAnimated:YES];
    //[theMovie release];
}
-(void)requestFinishedHandler:(ASIHTTPRequest*)request{
    NSMutableArray *arr=[XMLParser parseVideoDetail:request.responseData];
    //NSLog(@"movie_url:%@",[[arr objectAtIndex:0]objectAtIndex:0]);
    NSString *_videourl=(NSString*)[[arr objectAtIndex:0]objectAtIndex:0];
    //[player playMovieAtURL:[NSURL URLWithString:_videourl]];
    
    if(!player)player=[[MPMoviePlayerController alloc] initWithContentURL:[[NSURL alloc] initWithString:_videourl]];
    if ([player respondsToSelector:@selector(setAllowsAirPlay:)]) {
        [player setAllowsAirPlay:YES];
        
    }
    //if([player respondsToSelector:@selector(loadState)]){
    //}
    [player.view setFrame:playerController.view.bounds];
    [player setMovieSourceType:MPMovieSourceTypeStreaming];
    //[player setControlStyle:MPMovieControlStyleFullscreen];
    
    //[player.backgroundView addSubview:playerBack];
    [player play];
    [playerController.view addSubview:player.view];
    //[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playerCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:player];
    //[delegate playerDidLoad:player.view];
    //[player release];
}
-(void) playMovieURL:(NSString *)videoURL{
    //self.wantsFullScreenLayout=true;
    [[MyRequest shareInstance]loaddataWithURL:videoURL forObject:self];
    
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    //CGRect viewInsetRect = CGRectInset ([self.view bounds],0,0);
    NSLog(@"didRotate1");
    CGRect viewInsetRect=CGRectInset([playerController.view bounds], 0, 0);
	[[player view] setFrame:viewInsetRect];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    //return YES;
    return interfaceOrientation==UIInterfaceOrientationLandscapeRight;
    // Return YES for supported orientations
    //return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
